Makes an array with size slots, every one starting as fill. The kind of array follows from fill: array_create(5, 0) is an array
array_create(size, fill) -> array<T>| Name | Type |
|---|---|
size | int |
fill | element |
array<T>
Makes an array with size slots and puts fill in every one of them. array_create(5, 0) is five slots that all hold 0.
The kind of array follows from fill, so you never have to say it separately: 0 makes an array<int>, 0.0 an array<real>, true an array<bool>, "hi" an array<string>. A struct works too, and every slot gets its own copy.
So write fill as the kind you want. array_create(5, 0) is an array of whole numbers, and a decimal does not fit in one - scores[1] = lerp(...) or scores[1] = 0.5 is refused when the project is checked, and the message names the array_create line that made the array hold whole numbers. If the array will ever hold a decimal, make it with array_create(5, 0.0). The other way is fine: a whole number goes into an array of decimals as itself, 5 becoming 5.0.
size has to be a number you type, like 5. Not a variable. A GBA cannot hand out memory while a game runs - every array is given its room when the game is built, and a variable does not have a value yet at that point. If you use one, the compiler says so and explains it.
It sets the size of the variable it is given to, so it goes on the right-hand side of the line that creates the variable and nowhere else - not inside another expression. var scores = array_create(5, 0); in an event, or scores = array_create(5, 0); in Create for an instance variable.
Why bother, when /// @capacity 5 and [0, 0, 0, 0, 0] say the same? Two reasons. There is no comment involved - see Writing a type out for why that matters and the full list of ways to say it. And you never have to write the starting values out one by one: array_create(200, 0) is a 200-slot array in one short line.
It also fixes a trap with the older way. An array written as /// @capacity 5 with a literal of only three values holds just three slots, not five. The missing two are not zeros: they do not exist, and using [3] or [4] stops the game with an "Invalid index" message. An array<T> has no way to add slots later, so they never will. array_create(5, 0) always makes all five.
var scores = array_create(5, 0); // five slots, all 0
scores[2] = 99;
var third = scores[2]; // 99Compiles to a fixed-size vector of `size` items filled by the GBA module's own count-and-value constructor - the same storage `/// @type {array<T>}` + `/// @capacity size` gives, so the two spellings are interchangeable and this one is not slower or bigger. size has to be a plain number because it becomes a C++ template parameter: the Game Boy Advance has no heap to hand out memory while the game runs, so every array is sized when the ROM is built. A variable there is refused. It must be the whole right-hand side of a declaration - that is the one place a size can be attached to a variable.